﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Dal.Contract;
using Weight.Model;
using Weight.Model.ResultEntity;

namespace Weight.Bll.Contract
{
    public  class BllContractTicket
    {
        private readonly DalContractTicket Dal = new DalContractTicket();

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="mainId"></param>
        /// <param name="material"></param>
        /// <param name="isStart"></param>
        /// <returns></returns>
        public Tuple<bool,string, List<ContractTicketRes>> ListByMainId(string mainId,string material,string useStatus)
        {
            if(string.IsNullOrEmpty(mainId)) return new Tuple<bool, string, List<ContractTicketRes>>(false, "参数错误",null);
            var data =  Dal.ListByMainId(mainId,material, useStatus);
            return new Tuple<bool, string, List<ContractTicketRes>>(true,"成功",data);
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool,string> Add(ContractTicketEntity entity, ContractTicketRechargeEntity rechargeEntity)
        {
            if (entity == null) return new Tuple<bool, string>(false,"参数错误");
            bool b = Dal.Add(entity, rechargeEntity);
            if(b)
            {
                return new Tuple<bool, string>(true, "添加成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "添加失败");
            }
        }

        /// <summary>
        /// 选择订单
        /// </summary>
        /// <param name="contractNo"></param>
        /// <param name="customer"></param>
        /// <param name="material"></param>
        /// <returns></returns>
        public DataTable ListSelect(string contractNo, string customer,string material)
        {
            return Dal.ListSelect(contractNo, customer, material);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool,string> Update(ContractTicketEntity entity, ContractTicketRechargeEntity chargeEntity)
        {
            if (entity == null) return new Tuple<bool, string>(false,"参数错误");
            var oldEntity =  Dal.GetById(entity.Id);
            if(oldEntity.IsLocked) return new Tuple<bool, string>(false, "该订单已被锁死，无法修改,请联系管理员!\r\n锁死：有过磅记录或充值记录则锁死订单，避免修改造成的数据错乱；");
            bool b=  Dal.Update(entity, chargeEntity);
            if(b)
            {
                return new Tuple<bool, string>(true, "修改成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "修改失败");
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Tuple<bool,string, ContractTicketRes> GetById(string id)
        {
            if (string.IsNullOrEmpty(id)) return new Tuple<bool, string, ContractTicketRes>(false,"参数错误",null);
            var data =  Dal.GetById(id);
            if(data != null)
            {
                return new Tuple<bool, string, ContractTicketRes>(true, "查询成功", data);
            }
            else
            {
                return new Tuple<bool, string, ContractTicketRes>(false,"查询失败",null);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(string id)
        {
            return Dal.Del(id);
        }

        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Stop(string id)
        {
            return Dal.Stop(id);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Start(string id)
        {
            return Dal.Start(id);
        }

        /// <summary>
        /// 获取图表分析数据
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="where"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        public DataTable GetChartPieData(string fields, string where, string group)
        {
            if(string.IsNullOrEmpty(fields) || string.IsNullOrWhiteSpace(where) || string.IsNullOrWhiteSpace(group))
            {
                return null;
            }
            return Dal.GetChartPieData(fields,where,group);
        }
    }
}
